package cn.bx.bid.ctrl;

import cn.bx.bid.entity.AjaxResult;
import cn.bx.bid.entity.Page;
import cn.bx.bid.entity.User;
import cn.bx.bid.entity.Userrole;
import cn.bx.bid.service.RoleService;
import cn.bx.bid.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
public class UserController {
    @Autowired
    UserService userService;
    @Autowired
    RoleService roleService;

    @RequestMapping(value = "ulogin", produces = "application/json;charset=utf-8")
    @ResponseBody
    public AjaxResult login(HttpServletRequest request, @RequestParam("username") String name, @RequestParam("password") String pass) {
        User u = this.userService.login(name, pass);
        if (u != null) {
            request.getSession().setAttribute("USER_LOGIN", u);
            return AjaxResult.success(u);
        }
        return AjaxResult.error();
    }

    @RequestMapping(value = "usersearch", produces = "application/json;charset=utf-8")
    @ResponseBody
    public Page<User> search(@RequestParam(name = "page", defaultValue = "1") int pageNo, @RequestParam(name = "limit", defaultValue = "10") int limit, @RequestParam(name = "name", required = false) String name, @RequestParam(name = "startDate", required = false) Date start, @RequestParam(name = "endDate", required = false) Date end) {
        System.out.println(name);
        ModelAndView mv = new ModelAndView();
        Page<User> page = this.userService.paging(pageNo, limit, name, start, end);
        page.setPage(pageNo);
        return page;
    }

    @RequestMapping(value = "userAdd", produces = "application/json;charset=utf-8")
    @ResponseBody
    public AjaxResult reg(@ModelAttribute("u") User user, HttpServletRequest req) {
        System.out.println(user);
        int r = userService.add(user);
        if (r == 1)
            return AjaxResult.success();
        return AjaxResult.error();
    }

    @RequestMapping(value = "findRoles", produces = "application/json;charset=utf-8")
    @ResponseBody
    public AjaxResult findRoles() {
        List<Userrole> li = roleService.findALL();
        return AjaxResult.success(li);
    }

    @RequestMapping(value = "userGet", produces = "application/json;charset=utf-8")
    @ResponseBody
    public AjaxResult userGet(@RequestParam(name = "id") long id) {
        User user = this.userService.get(id);
        if (user != null)
            return AjaxResult.success(user);
        return AjaxResult.error();
    }

    @RequestMapping(value = "userUpdate", produces = "application/json;charset=utf-8")
    @ResponseBody
    public AjaxResult userUpdate(@ModelAttribute("u") User user) {
        int r = userService.update(user);
        if (r == 1)
            return AjaxResult.success();
        return AjaxResult.error();
    }

    @RequestMapping(value = "/batchDel", produces = "application/json;charset=utf-8")
    @ResponseBody
    public AjaxResult batchDel(@RequestParam(name = "ids[]") long ar[]){
//        long[] ids = new long[ar.length];
//        for (int i = 0; i < ids.length; i++)
//            ids[i] = Long.parseLong(ar[i]);
//        System.out.println(Arrays.toString(ids) + ">>>>被删除了。。。");
        int r = userService.batchDel(ar);
        //int r = (int) (System.nanoTime() % 10);//假删除
        if(r>0)
        return AjaxResult.success(r);
        return AjaxResult.error();
    }
    @RequestMapping(value = "/checkUserNameRepeat", produces = "application/json;charset=utf-8")
    @ResponseBody
    public AjaxResult checkUserNameRepeat(@RequestParam(name = "userName") String name, @RequestParam(name = "id", required = false) Long id){
        boolean repeat = this.userService.checkUserNameRepeat(id, name);
        if(repeat)
        return AjaxResult.error();
        return AjaxResult.success();
    }

    @InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
        // true:允许输入空值，false:不能为空值
    }

//    @ExceptionHandler({ArithmeticException.class})
//    public String arithmeticException(Exception e){
//        System.out.println("arithmeticException:"+e);
//        return "error";
//    }

}
